<?php

namespace Drupal\mxm_article\Entity;

use Entity;

class MxmArticle extends Entity {

  public $id;

  public $vid;

  public $uid;

  public $op_uid;

  public $created;

  public $changed;

  public $is_new_revision;

  public $title;

  public $status = 1;

  public $sort = 0;

  public $promote = 0;

  public $announcement = 0;

  public $window_index = 0;

  public $summary;

  function __construct($values = []) {
    $this->is_new_revision = TRUE;
    parent::__construct($values, 'mxm_article');
  }

  public function save() {
    $this->changed = REQUEST_TIME;
    if (empty($this->created)) {
      $this->created = REQUEST_TIME;
    }

    $this->op_uid = $GLOBALS['user']->uid;
    if (empty($this->uid)) {
      $this->uid = $GLOBALS['user']->uid;
    }

    //    if ($this->window_index == 1) {
    //      //关闭其它弹窗
    //      $es = mxm_article_load_multiple(FALSE, ['window_index' => 1]);
    //      foreach ($es as $e) {
    //        if ($e instanceof MxmArticle) {
    //          if ($e->id != $this->id) {
    //            $e->window_index = 0;
    //            $e->save();
    //          }
    //        }
    //      }
    //    }

    parent::save();
    variable_set("mxm_article:window_index", 0);
    $es = mxm_article_load_multiple(FALSE, ['window_index' => 1]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof MxmArticle) {
        variable_set("mxm_article:window_index", $e->id);
      }
    }
  }

}
